package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /*
     * @User Administrator
     * @Description //TODO  查询商品列表
     * @Date 9:46 2019/8/13 0013
     * @Param
     * @return
     **/
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "key", required = false) String key) {
        return ResponseEntity.ok(goodsService.querySpuByPage(page, rows, saleable, key));
    }

    /**
     * 新增商品
     *
     * @param spuDTO 页面提交商品信息  (新增商品提交功能)
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        this.goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /*
     * @User Administrator
     * @Description //TODO  修改上下架
     * @Date 11:01 2019/8/13 0013
     * @Param
     * @return
     **/
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long id,
                                                   @RequestParam(value = "saleable",defaultValue = "true") Boolean saleable) {
        goodsService.updateSpuSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /*
     * @User Administrator
     * @Description //TODO  修改商品信息  回显
     * @Date 11:01 2019/8/13 0013
     * @Param
     * @return
     **/
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {
        return ResponseEntity.ok(goodsService.querySpuDetailById(id));
    }

    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> findSkusBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(goodsService.findSkusBySpuId(id));
    }

    /*
     * @User Administrator
     * @Description //TODO 回显修改之后的 保存
     * @Date 11:19 2019/8/13 0013
     * @Param
     * @return
     **/
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据spu的id查询spu
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }
}
